package com.carkeeper.blog.service.impl;

import com.carkeeper.blog.dao.BlogMapper;
import com.carkeeper.blog.pojo.Blog;
import com.carkeeper.blog.service.BlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: carkeeper_parent
 * @Date: 2020/11/23 14:18
 * @Author: qgg
 * @Description:
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    /**
     * 查询所有，当进入该页面时查询所有数据
     * @return
     */
    @Override
    public List<Blog> findAll() {
        return blogMapper.selectAll();
    }

    //多条件查询
    @Override
    public List<Blog> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);

        return blogMapper.selectByExample(example);
    }

    //分页查询
    @Override
    public PageInfo<Blog> findPage(int pageNum, int pageSize) {
        //设置分页条件
        PageHelper.startPage(pageNum, pageSize);
        //返回查询数据
        List<Blog> blogList = blogMapper.selectAll();
        PageInfo pageInfo = new PageInfo(blogList);
        return pageInfo;
    }

    //新增
    @Override
    public void add(Blog blog) {
        blogMapper.insertSelective(blog);
    }

    //删除
    @Override
    public void delete(String id){
        blogMapper.deleteByPrimaryKey(id);
    }

    //修改
    @Override
    public void update(Blog blog){
        blogMapper.updateByPrimaryKey(blog);
    }

    //根据id查询数据
    @Override
    public Blog findById(String id){
        Blog blog = blogMapper.selectByPrimaryKey(id);
        return blog;
    }

    //改变状态
    @Override
    public void updateStatus(String id){
        Blog status_blog = blogMapper.selectByPrimaryKey(id);

        //获取数据库中修改时间
        String updateTime = status_blog.getUpdateTime();

        //获取当前状态
        Integer status = status_blog.getStatus();

        //当前修改操作的时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        updateTime = format.format(date);

        if (status == 1){
            //进来表示此时状态是未发布，将status改为2，表示已发布
            status =2;
            status_blog.setStatus(status);
            status_blog.setUpdateTime(updateTime);
        }else {
            //进来表示此时状态是未发布，将status改为2，表示已发布
            status =1;
            status_blog.setStatus(status);
            status_blog.setUpdateTime(updateTime);
        }
        blogMapper.updateByPrimaryKeySelective(status_blog);

    }

    //设置多条件查询条件
    private Example createExample(Map<String, Object> searchMap) {

        Example example = new Example(Blog.class);

        Example.Criteria criteria = example.createCriteria();

        if (searchMap != null){
            //标题
            if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))){
                criteria.andEqualTo("title" , searchMap.get("title"));
            }

            //所属分类
            if (searchMap.get("blogCategoryId") != null && !"".equals(searchMap.get("blogCategoryId"))){
                criteria.andEqualTo("blogCategoryId" , searchMap.get("blogCategoryId"));
            }

            //发布人
            if (searchMap.get("creator") != null && !"".equals(searchMap.get("creator"))){
                criteria.andEqualTo("creator" , searchMap.get("creator"));
            }

            //创建时间
            if (searchMap.get("createTime") != null && !"".equals(searchMap.get("createTime"))){
                criteria.andEqualTo("createTime" , searchMap.get("createTime"));
            }

            //状态
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))){
                criteria.andEqualTo("status" , searchMap.get("status"));
            }
        }
        return example;
    }
}
